<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <link rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" onerror="_cdnFallback(this)">
    <link type="text/css" rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/zepto.alert.css" />
    <link type="text/css" rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/wish/index.css" />
    <script type='text/javascript' src='{$Think.config.RESOURCEURL}wx_assets/js/zepto.1.1.3.min.js'></script>
    <script type='text/javascript' src='{$Think.config.RESOURCEURL}wx_assets/js/json2.js'></script>
    <script src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js" type="text/javascript"></script>
    <script src="{$Think.config.RESOURCEURL}wx_assets/js/util.js" type="text/javascript"></script>

</head>
<body>
    <div class="page">
        <div class="address-box" id="AddressBox">
            <ul id="Address">
                <li>
                    <div class="fl">
                        收件人：<label id="addr_fullName">{$userInfo.fullName}</label></div>
                        <input type="hidden" id="hid_fullName" value="{$userInfo.fullName}" />
                        <input type="hidden" id="hid_mobile" value="{$userInfo.phone}" />
                        <input type="hidden" id="hid_address" value="{$userInfo.address}" />
                    <div class="fr">
                        <label id="addr_phone">{$userInfo.phone}</label></div>
                </li>
                <li>
                    收货地址：<label id="addr_detail">{$userInfo.address}</label>
                </li>
            </ul>
            <em></em>
        </div>
        <div class="wish-write">
            <div class="title">
                <h2>
                    我想说的话：</h2>
                <a id="btnSwitch">随机换一段</a>
            </div>
            <div>
                <textarea class="input-txt" id="wishText"></textarea>
            </div>
        </div>
        <div class="wish-pros" id="WishPros">
            <foreach name="proList" item="vo">
                <dl>
                    <dt>
                        <a href='{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&module=product&method=getProduct&params=%7B"id"%3A"{$vo.id}"%7D'><img src="{$vo.picUrl}" /></a></dt>
                    <dd>
                        <div class="pro-pros pro-pros-create">
                            <div class="pro-name">
                                <a href='{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&module=product&method=getProduct&params=%7B"id"%3A"{$vo.id}"%7D'>{$vo.name}</a>
                            </div>
                            <div class="pro-price">
                                <if condition="$vo.inventory eq '1' and $vo.inventoryCount lt 1">
                                    <br />
                                    <span>已售罄</span>
                                <else/>
                                    市场价：<span class="s">￥{$vo.fixedPrice}</span>
                                    <if condition="$vo.discountedPrice neq ''">
                                    <br />
                                    促销价：<span>￥{$vo.discountedPrice}</span><em class="checkbox checked" data-id="{$vo.id}"></em>
                                    </if>
                                </if>
                            </div>
                        </div>
                    </dd>
                </dl>
            </foreach>
        </div>
    </div>
    <div class="page-fixed">
        <a class="create-btn" id="createBtn" style="cursor:pointer">分享我的心愿单</a>
    </div>
    <script type="text/javascript">
        var baseUrl = '{$Think.config.SERVER_HOST}';
        function setAddr(b){
            var customerId = $.util.getParam("customerId");
            var userName = $.util.getParam("userName");
            if (typeof WeixinJSBridge == 'undefined'){
               var t= setTimeout(function(){
                    setAddr(b);
                },10);
                return false;
              }else{
                WeixinJSBridge.invoke('editAddress', {
                    "appId": b.appId,
                    "scope" : b.scope,
                    "signType": b.signType,
                    "addrSign": b.addrSign,
                    "timeStamp": b.timeStamp,
                    "nonceStr": b.nonceStr
                },function(res){
                    if(res.err_msg != 'edit_address:ok'){
//                        if (res.err_msg == 'edit_address:fail') {
//                            return;
//                        } else {
//                            window.top.location.href=baseUrl+'call.html?customerId={$customerId}&module=wish&method=getAddrEdit&params=%7B"actId"%3A"{$actId}"%7D';
//                        }
                        window.top.location.href=baseUrl+'call.html?customerId={$customerId}&module=wish&method=getAddrEdit&params=%7B"actId"%3A"{$actId}"%7D';
                    }
                    
                    window.fullName = res.userName;
                    window.phone = res.telNumber;
                    var addr = res.addressCitySecondStageName + res.proviceFirstStageName + res.addressDetailInfo;
                    if( addr ) {
                        $("#addr_fullName").html(res.userName);
                        $("#addr_phone").html(res.telNumber);
                        $("#addr_detail").html(addr);
                        $("#hid_fullName").val(res.userName);
                        $("#hid_mobile").val(res.telNumber);
                        $("#hid_address").val(addr);
                    } else {
                        window.top.location.href=baseUrl+'call.html?customerId={$customerId}&module=wish&method=getAddrEdit&params=%7B"actId"%3A"{$actId}"%7D';
                    }
                });
                clearTimeout(t);
            } 
        }
        var wish = {
            wishText:'{$wishConfig.wishTexts}',
            rnd:0,
            init: function() {
                this._initWishBox();
                this._initBtnEvent();
                this._initCheckBox();
                this._initWish();
            },
            _initWish:function(){
                var me=this;
                $("#btnSwitch").click(function(){
                    if(me.wishText) {
                        var wishTexts=me.wishText.split("|");
                        if(me.rnd >= wishTexts.length-1) {
                            me.rnd=0;
                        }
                        $("#wishText").val(wishTexts[me.rnd]);
                    }
                })
                .trigger("click")
            },
            _save:function(){
                var pids=[];
                var customerId = $.util.getParam("customerId");
                var userName = $.util.getParam("userName");
                var postUrl = baseUrl + 'call.json?customerId={$customerId}';
                $("#WishPros .checkbox").each(function() {
                    if($(this).hasClass("checked")){
                        pids.push($(this).attr("data-id"));
                    }
                });
                if(pids.length<1) {
                    $.util.alert('请选择商品！');
                    $("#createBtn").removeClass("btn_disabled");
                    return;
                }
                var fullName=$("#hid_fullName").val();
                if(!fullName){
                     $.util.alert('请首先设置您的收货地址!');
                     $("#createBtn").removeClass("btn_disabled");
                    return;
                }
                var data= {
                    actId:{$actId},
                    pids:pids.join(","),
                    wishText:$("#wishText").val(),
                    fullName:fullName,
                    mobile:$("#hid_mobile").val(),
                    address:$("#hid_address").val()
                }
                $.util.pageLoading.show('保存中，请稍候！');
                $.ajax({
                    url: postUrl,
                    type: "post",
                    dataType: "json",
                    data: {
                        "module": "wish",
                        "method": "saveWish",
                        "userName":"{$userName}",
                        "params": JSON.stringify(data)
                    },
                    success: function(data) {
                        $.util.pageLoading.hide();
                        if (data && data.code) {
                            $.util.alert(data.msg);
                            $("#createBtn").removeClass("btn_disabled");
                        } else {
                           window.top.location.href=baseUrl+'call.html?customerId={$customerId}&module=wish&show=1&&method=index&params=%7B"actId"%3A"{$actId}"%7D';
                        }
                    },
                    error:function(){
                        $("#createBtn").removeClass("btn_disabled");
                    }
                });
            },
            _initBtnEvent: function() {
                var me=this;
                $("#createBtn").click(function(){
                    if(!$(this).hasClass("btn_disabled")){
                        $(this).addClass("btn_disabled")
                        me._save();
                    }
                });
                $("#Address").click(function(){
                    me._getAddr();
                });
            },
            _initCheckBox: function() {
                $("#WishPros .checkbox").click(function() {
                    $(this).toggleClass("checked");
                });
            },
            _initWishBox: function() {
                var maxWidth = $(window).width();
                $("#UserWish").css("width", Math.ceil(maxWidth / 2) - 60 + 'px');
            },
            _getAddr:function(){
                var customerId = $.util.getParam("customerId");
                var userName = $.util.getParam("userName");
                var postUrl = baseUrl + 'call.json?customerId={$customerId}';
                $.ajax({
                    url: postUrl,
                    dataType: "json",
                    type: "POST",
                    timeout: 15000,
                    data: {
                        'module': 'product',
                        'method': 'getAddrSign',
                        'params': '{"url":"' + encodeURIComponent(document.location.href) + '"}'
                    },
                    success:function(b){
                        if (typeof(b.code) == 'undefined' ) {
                            try{
                               setAddr(b);
                            }
                            catch(e){
                                $.util.alert("操作失败，请确保在微信浏览器中操作");
                                return false;
                            }
                        }
                    }
                });
            }
        }
        wish.init();
    </script>

</body>
</html>
